﻿using System;
using System.Linq;
using WebExam.DataEntities;
using WebExam.DataProvider;

namespace WebExam.SQLDataProvider
{
    public class CJRepository : BaseRepository<CJ>, ICJRepository
    {
        /// <summary>
        /// 根据学号获取学生成绩
        /// </summary>
        /// <param name="xsId">学号</param>
        /// <param name="start">开始位置</param>
        /// <param name="takeSize">获取条数</param>
        public IQueryable<CJ> GetAll(string xsId, int start, int takeSize)
        {
            using(ThisDb)
            {
                return ThisTable.Where(p => p.XSId == xsId).OrderByDescending(p => p.Date).Skip(start).Take(takeSize);
            }
        }

        /// <summary>
        /// 根据成绩单Id获取学生成绩
        /// </summary>
        /// <param name="cjdId">成绩单Id</param>
        public CJ Get(int cjdId)
        {
            using (ThisDb)
            {
                return ThisTable.SingleOrDefault(p => p.CJDNum==cjdId);
            }
        }

        /// <summary>
        /// 添加学生成绩
        /// </summary>
        /// <param name="cj">成绩对象</param>
        public CJ Add(CJ cj)
        {
            using (ThisDb)
            {
                try
                {
                    ThisTable.InsertOnSubmit(cj);
                    ThisDb.SubmitChanges();
                    return cj;
                }
                catch
                {
                    return cj;
                }
            }
        }

        /// <summary>
        /// 根据学号统计成绩条数
        /// </summary>
        /// <param name="xsId">学号</param>
        public int Count(string xsId)
        {
            using (ThisDb)
            {
                return ThisTable.Count(p => string.Equals(p.XSId, xsId, StringComparison.Ordinal));
            }
        }
    }
}